package com.ysd.lis.service.bac.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacStainingOptions;
import com.ysd.lis.mapper.bac.BacStainingOptionsMapper;
import com.ysd.lis.request.Params;
import com.ysd.lis.service.bac.BacStainingOptionsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wanghuaixing
 * @since 2024-04-08
 */
@Service
public class BacStainingOptionsServiceImpl extends ServiceImpl<BacStainingOptionsMapper, BacStainingOptions> implements BacStainingOptionsService {

    @Autowired
    BacStainingOptionsMapper optionsMapper;

    @Override
    public Result findStainingOptionsList(Params params) {
        LambdaQueryWrapper<BacStainingOptions> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BacStainingOptions::getDelFlag, 0);
        if (!StringUtils.isEmpty(params.getSearchValue())) {
            queryWrapper.and(wrapper -> wrapper.like(BacStainingOptions::getResultCode, params.getSearchValue()).or().like(BacStainingOptions::getResultName, params.getSearchValue()));
        }
        queryWrapper.eq(BacStainingOptions::getStaConclusionNo, params.getStainingConclusionId());
        List<BacStainingOptions> optionsList = optionsMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", optionsList);
    }

    @Override
    public Result batchSaveStainingOptions(List<BacStainingOptions> optionsList) {
        if (optionsList.isEmpty()) {
            return Result.fail("没有要更新的项");
        }
        List<BacStainingOptions> nList = optionsList.stream().filter(p -> com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(p.getId())).collect(Collectors.toList());
        List<BacStainingOptions> uList = optionsList.stream().filter(p -> com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(p.getId())).collect(Collectors.toList());
        if (!nList.isEmpty()) {
            boolean r = this.saveBatch(nList);
        }
        if (!uList.isEmpty()) {
            boolean r1 = this.updateBatchById(uList);
        }
        return Result.succ(1, "操作成功", optionsList);
    }

    @Override
    public Result batchDeleteStainingOptions(List<BacStainingOptions> optionsList) {
        List<String> ids = optionsList.stream().map(BacStainingOptions::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }
}
